local string = string

function rgb_to_r_g_b(color, alpha) -- 将0x123456这样的RGB转成[0,1]间的值 alpha值表示不变
    return ((color / 0x10000) % 0x100) / 255., ((color / 0x100) % 0x100) / 255., (color % 0x100) / 255., alpha
end

-- 一些变量的默认设置
default_fg_color, default_bg_color, emph_color = 0xDDDDFF, 0xDDDDFF, 0x00D700 -- 前景色 背景色 高亮色
default_font_size = 38 -- 圆环中心文字的字号

time_thick_base, time_thick_inc = 12, 4
time_thick = {time_thick_base - time_thick_inc, time_thick_base, time_thick_base + time_thick_inc} -- 时 分 秒 的圆环宽度

time_radius = {99, 120, 144} -- 时 分 秒 的半径

date_thick_base, date_thick_inc = 20, -4
date_thick = {date_thick_base - date_thick_inc, date_thick_base, date_thick_base + date_thick_inc} -- 星期 月份 天数 的圆环宽度

date_radius_base, date_radius_inc = 174, -2
date_radius = {date_radius_base - date_radius_inc, date_radius_base, date_radius_base + date_radius_inc} -- 星期 月份 天数 的半径

date_angle_base, date_angle_inc = 100, 120
date_start = {date_angle_base - date_angle_inc, date_angle_base, date_angle_base + date_angle_inc} -- 星期 月份 天数 的半径起始角度

clock_alpha = {
    {bg_alpha = 0.2, fg_alpha = 0.8}, --时
    {bg_alpha = 0.2, fg_alpha = 0.8}, --分
    {bg_alpha = 0.2, fg_alpha = 0.8}, --秒
    {bg_alpha = 0.2, fg_alpha = 0.8}, --星期
    {bg_alpha = 0.2, fg_alpha = 0.8}, --月份
    {bg_alpha = 0.2, fg_alpha = 0.8} --日子
}
clock_gap = 2
clock_x, clock_y = date_radius_base + (date_thick_base - date_thick_inc) / 2, 250 -- clock圆环的圆心坐标

cpu_x, cpu_y = date_radius_base + (date_thick_base - date_thick_inc) / 2, 700 -- cpu圆环的圆心坐标
cpu_thick = {12, 12, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8}
cpu_radius = {120, 140, 138, 150, 162, 174, 138, 150, 162, 174, 138, 150, 162, 174, 138, 150, 162, 174}
cpu_core_end, cpu_core_len, cpu_core_gap = 50, 30, 5
cpu_core_start = {
    cpu_core_end - 7 * cpu_core_len - 4 * cpu_core_gap, -- 温度环的开始角度 弧度等于单个核的3倍
    cpu_core_end - 4 * cpu_core_len - 3 * cpu_core_gap, -- 第1-4个核的开始角度
    cpu_core_end - 3 * cpu_core_len - 2 * cpu_core_gap, -- 第5-8个核的开始角度
    cpu_core_end - 2 * cpu_core_len - 1 * cpu_core_gap, -- 第9-12个核的开始角度
    cpu_core_end - 1 * cpu_core_len -- 第13-16个核的开始角度
}
cpu_bg_alpha, cpu_fg_alpha = 0.25, 0.9

mem_x, mem_y = date_radius_base + (date_thick_base - date_thick_inc) / 2, 1120

disk_x, disk_y = 370, 1530
disk_thick_ = 12
disk_radius_ = 60
disk_thick = {disk_thick_, disk_thick_, disk_thick_}
disk_radius = {disk_radius_, disk_radius_, disk_radius_}
disk_start = {0, 120, 240}

net_x, net_y = 370, 1900
ip_shift = 180
net_thick = 12
net_radius_small = 60
net_radius_large = 72

require "cairo"

rings_table = {
    {
        name = "time",
        arg = "%I.%M",
        max = 12,
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[1]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[1]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = time_radius[1],
        thick = time_thick[1],
        start_angle = 0,
        end_angle = 360,
        graduated = true
    },
    {
        name = "time",
        arg = "%M.%S",
        max = 60,
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[2]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[2]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = time_radius[2],
        thick = time_thick[2],
        start_angle = 0,
        end_angle = 360,
        graduated = true
    },
    {
        name = "time",
        arg = "%S",
        max = 60,
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[3]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[3]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = time_radius[3],
        thick = time_thick[3],
        start_angle = 0,
        end_angle = 360,
        graduated = true
    },
    -- 星期的处理
    {
        name = "time",
        arg = "%u",
        max = 7,
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[4]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[4]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = date_radius[1],
        thick = date_thick[1],
        start_angle = date_start[1],
        end_angle = date_start[1] + 110,
        graduated = true
    },
    -- 月份的处理
    {
        name = "time",
        arg = "%m",
        max = 12,
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[5]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[5]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = date_radius[2],
        thick = date_thick[2],
        start_angle = date_start[2],
        end_angle = date_start[2] + 110,
        graduated = true
    },
    -- 天数的处理
    {
        name = "time",
        arg = "%d",
        max = 1000, --随便设 后面在main函数中根据当前月份修改
        bg_color = default_bg_color,
        bg_alpha = clock_alpha[6]["bg_alpha"],
        fg_color = default_fg_color,
        fg_alpha = clock_alpha[6]["fg_alpha"],
        x = clock_x,
        y = clock_y,
        radius = date_radius[3],
        thick = date_thick[3],
        start_angle = date_start[3],
        end_angle = date_start[3] + 110,
        graduated = true
    },
    -- cpu0 270°
    {
        name = "cpu",
        arg = "cpu0",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[1],
        thick = cpu_thick[1],
        start_angle = -180,
        end_angle = 90,
        graduated = false
    },
    -- cpu温度
    {
        name = "platform",
        arg = "coretemp.0/hwmon/hwmon2 temp 1",
        max = 110,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[2],
        thick = cpu_thick[2],
        start_angle = cpu_core_start[1],
        end_angle = cpu_core_start[1] + 3 * cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu1",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[3],
        thick = cpu_thick[3],
        start_angle = cpu_core_start[2],
        end_angle = cpu_core_start[2] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu2",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[4],
        thick = cpu_thick[4],
        start_angle = cpu_core_start[2],
        end_angle = cpu_core_start[2] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu3",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[5],
        thick = cpu_thick[5],
        start_angle = cpu_core_start[2],
        end_angle = cpu_core_start[2] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu4",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[6],
        thick = cpu_thick[6],
        start_angle = cpu_core_start[2],
        end_angle = cpu_core_start[2] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu5",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[3],
        thick = cpu_thick[3],
        start_angle = cpu_core_start[3],
        end_angle = cpu_core_start[3] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu6",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[4],
        thick = cpu_thick[4],
        start_angle = cpu_core_start[3],
        end_angle = cpu_core_start[3] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu7",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[5],
        thick = cpu_thick[5],
        start_angle = cpu_core_start[3],
        end_angle = cpu_core_start[3] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu8",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[6],
        thick = cpu_thick[6],
        start_angle = cpu_core_start[3],
        end_angle = cpu_core_start[3] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu9",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[3],
        thick = cpu_thick[3],
        start_angle = cpu_core_start[4],
        end_angle = cpu_core_start[4] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu10",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[4],
        thick = cpu_thick[4],
        start_angle = cpu_core_start[4],
        end_angle = cpu_core_start[4] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu11",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[5],
        thick = cpu_thick[5],
        start_angle = cpu_core_start[4],
        end_angle = cpu_core_start[4] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu12",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[6],
        thick = cpu_thick[6],
        start_angle = cpu_core_start[4],
        end_angle = cpu_core_start[4] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu13",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[3],
        thick = cpu_thick[3],
        start_angle = cpu_core_start[5],
        end_angle = cpu_core_start[5] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu14",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[4],
        thick = cpu_thick[4],
        start_angle = cpu_core_start[5],
        end_angle = cpu_core_start[5] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu15",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[5],
        thick = cpu_thick[5],
        start_angle = cpu_core_start[5],
        end_angle = cpu_core_start[5] + cpu_core_len,
        graduated = false
    },
    {
        name = "cpu",
        arg = "cpu16",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = cpu_x,
        y = cpu_y,
        radius = cpu_radius[6],
        thick = cpu_thick[6],
        start_angle = cpu_core_start[5],
        end_angle = cpu_core_start[5] + cpu_core_len,
        graduated = false
    },
    -- 内存
    {
        name = "memperc",
        arg = "",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = cpu_bg_alpha,
        fg_color = default_fg_color,
        fg_alpha = cpu_fg_alpha,
        x = mem_x,
        y = mem_y,
        radius = cpu_radius[1],
        thick = cpu_thick[1],
        start_angle = -180,
        end_angle = 90,
        graduated = false
    },
    -- 硬盘
    {
        name = "fs_used_perc",
        arg = "/home",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.7,
        x = disk_x,
        y = disk_y,
        radius = disk_radius[1],
        thick = disk_thick[1],
        start_angle = disk_start[1],
        end_angle = disk_start[1] + 110,
        graduated = false
    },
    {
        name = "fs_used_perc",
        arg = "/efi",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.8,
        x = disk_x,
        y = disk_y,
        radius = disk_radius[2],
        thick = disk_thick[2],
        start_angle = disk_start[2],
        end_angle = disk_start[2] + 110,
        graduated = false
    },
    {
        name = "fs_used_perc",
        arg = "/",
        max = 100,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.9,
        x = disk_x,
        y = disk_y,
        radius = disk_radius[3],
        thick = disk_thick[3],
        start_angle = disk_start[3],
        end_angle = disk_start[3] + 110,
        graduated = false
    },
    -- 有线 上传
    {
        name = "upspeedf",
        arg = "ens20u1u1",
        max = 12500,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.9,
        x = net_x,
        y = net_y,
        radius = net_radius_small,
        thick = net_thick,
        start_angle = 270,
        end_angle = 360,
        graduated = false
    },
    -- 有线 下载
    {
        name = "downspeedf",
        arg = "ens20u1u1",
        max = 12500,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.9,
        x = net_x,
        y = net_y,
        radius = net_radius_large,
        thick = net_thick,
        start_angle = 180,
        end_angle = 270,
        graduated = false
    },
    -- 无线 下载
    {
        name = "downspeedf",
        arg = "wlp59s0",
        max = 12500,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.7,
        x = net_x,
        y = net_y,
        radius = net_radius_small,
        thick = net_thick,
        start_angle = 90,
        end_angle = 180,
        graduated = false
    },
    -- 无线 上传
    {
        name = "upspeedf",
        arg = "wlp59s0",
        max = 12500,
        bg_color = default_bg_color,
        bg_alpha = 0.3,
        fg_color = default_fg_color,
        fg_alpha = 0.7,
        x = net_x,
        y = net_y,
        radius = net_radius_large,
        thick = net_thick,
        start_angle = 0,
        end_angle = 90,
        graduated = false
    }
}

function draw_ring(cr, value, pt) -- t是百分比 pt是个table
    local w, h = conky_window.width, conky_window.height

    local name, xc, yc, ring_r, ring_w, sa, ea, max, graduated =
        pt["name"],
        pt["x"],
        pt["y"],
        pt["radius"],
        pt["thick"],
        pt["start_angle"],
        pt["end_angle"],
        pt["max"],
        pt["graduated"]

    local bgc, bga, fgc, fga = pt["bg_color"], pt["bg_alpha"], pt["fg_color"], pt["fg_alpha"]

    local angle_0 = sa * (2 * math.pi / 360) - math.pi / 2 -- 起始弧度
    local angle_f = ea * (2 * math.pi / 360) - math.pi / 2 -- 结束弧度

    cairo_set_line_width(cr, ring_w)

    if value > max then
        value = value - max
    end

    -- 先画背景环
    if graduated then
        for i = 1, max do
            local angle_start = angle_0 + (i - 1) * (ea - sa) * 2 * math.pi / (max * 360)
            local angle_end = angle_0 + (i * (ea - sa) / max - clock_gap) * 2 * math.pi / 360
            cairo_arc(cr, xc, yc, ring_r, angle_start, angle_end)
            cairo_set_source_rgba(cr, rgb_to_r_g_b(bgc, bga))
            cairo_stroke(cr)
        end
    else
        cairo_arc(cr, xc, yc, ring_r, angle_0, angle_f)
        cairo_set_source_rgba(cr, rgb_to_r_g_b(bgc, bga))
        cairo_stroke(cr)
    end

    -- 后画前景环
    if graduated then
        for i = 1, value do
            local angle_start = angle_0 + (i - 1) * (ea - sa) * 2 * math.pi / (max * 360)
            local angle_end = angle_0 + (i * (ea - sa) / max - clock_gap) * 2 * math.pi / 360
            cairo_arc(cr, xc, yc, ring_r, angle_start, angle_end)
            cairo_set_source_rgba(cr, rgb_to_r_g_b(fgc, fga))
            cairo_stroke(cr)
        end
    else
        -- 获得百分比
        if max == 12500 then --如果是网络 采用对数拉伸
            if value < 1 then
                t = 0
            else
                t = math.log10(value) / math.log10(max)
            end
        else
            t = value / max
        end
        local t_arc = t * (angle_f - angle_0) -- 百分比 * 2 pi
        if (name == "downspeedf" and sa == 180) or (name == "upspeedf" and sa == 0) then
            cairo_arc(cr, xc, yc, ring_r, angle_f - t_arc, angle_f)
        else
            cairo_arc(cr, xc, yc, ring_r, angle_0, angle_0 + t_arc)
        end
        cairo_set_source_rgba(cr, rgb_to_r_g_b(fgc, fga))
        cairo_stroke(cr)
    end
end

function draw_line(cr, xc_start, yc_start, xc_end, yc_end, width, trans)
    cairo_set_source_rgba(cr, rgb_to_r_g_b(default_fg_color, trans))
    cairo_move_to(cr, xc_start, yc_start)
    cairo_line_to(cr, xc_end, yc_end)
    cairo_set_line_width(cr, width)
    cairo_stroke(cr)
end

function draw_left_text(cr, xc, yc, str, font_color, font_name, font_size) --用cairo_show_text默认就是从左开始
    cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT)
    cairo_set_font_size(cr, font_size)
    cairo_move_to(cr, xc, yc)
    cairo_set_source_rgba(cr, rgb_to_r_g_b(font_color, 1.0))
    cairo_show_text(cr, str)
end

function draw_center_text(cr, xc, yc, str, font_color, font_name, font_size) --居中显示文本
    local extents = cairo_text_extents_t:create()
    tolua.takeownership(extents)
    cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT)
    cairo_set_font_size(cr, font_size)
    cairo_text_extents(cr, str, extents)
    local x = xc - (extents.width / 2 + extents.x_bearing)
    local y = yc - (extents.height / 2 + extents.y_bearing)
    cairo_move_to(cr, x, y)
    cairo_set_source_rgba(cr, rgb_to_r_g_b(font_color, 1.0))
    cairo_show_text(cr, str)
end

function draw_right_text(cr, xc, yc, str_arr, font_color_arr, font_name, font_size) --居右显示文本 此时横坐标参数参数xc不起作用
    local extents = cairo_text_extents_t:create()
    tolua.takeownership(extents)
    cairo_select_font_face(cr, font_name, CAIRO_FONT_SLANT_ITALIC, CAIRO_FONT_WEIGHT)
    cairo_set_font_size(cr, font_size)
    local x_start = conky_window.width
    for i = #str_arr, 1, -1 do
        cairo_text_extents(cr, str_arr[i], extents)
        x_start = x_start - (extents.width + 2 * extents.x_bearing)
        cairo_move_to(cr, x_start, yc)
        cairo_set_source_rgba(cr, rgb_to_r_g_b(font_color_arr[i], 1.0))
        cairo_show_text(cr, str_arr[i]:gsub("+", " "))
    end
end

function draw_process_text(cr, xc, yc, process_num, command, font_name, font_size)
    local str = "-PID-" .. string.rep(" ", 3) .. "-PER-"
    local x_shift = 60
    local y_shift = 60
    local str_arr
    draw_right_text(cr, xc + x_shift, yc + y_shift, {str}, {default_fg_color}, font_name, font_size)

    for i = 1, process_num do
        local name, pid, value
        if command == "cpu" then
            name = conky_parse("${top name " .. i .. "}"):sub(1, 10)
            pid = conky_parse("${top pid " .. i .. "}")
            value = conky_parse("${top " .. command .. " " .. i .. "}") .. "%"
            value = value:gsub(" ", "")
        else
            name = conky_parse("${top_" .. command .. " name " .. i .. "}"):sub(1, 10)
            pid = conky_parse("${top_" .. command .. " pid " .. i .. "}")
            value = conky_parse("${top_" .. command .. " " .. command .. " " .. i .. "}") .. "%"
            value = value:gsub(" ", "")
        end
        draw_right_text(
            cr,
            0,
            yc + y_shift + 40 * i,
            {
                name ..
                    string.rep(" ", 17 - string.len(name) - string.len(pid)) ..
                        pid .. string.rep(" ", 8 - string.len(value)) .. value
            },
            {default_fg_color},
            font_name,
            font_size
        )
    end
end

function draw_disk_text(cr, x_shift, y_shift, command, disk_name, font_name, font_size, align)
    local str_disk = conky_parse("${fs_used " .. command .. "} / ${fs_size " .. command .. "}"):gsub("iB", "")
    local str_disk_perc = conky_parse("${fs_used_perc " .. command .. "}") .. "%"
    local space_len = string.len(str_disk) - string.len(str_disk_perc) - string.len(disk_name)
    if align == "l" then
        draw_left_text(
            cr,
            0,
            disk_y + y_shift,
            disk_name .. string.rep(" ", space_len) .. str_disk_perc,
            default_fg_color,
            font_name,
            font_size
        )
        draw_left_text(cr, 0, disk_y + y_shift + 40, str_disk, default_fg_color, font_name, font_size)
    elseif align == "r" then
        draw_right_text(
            cr,
            0,
            disk_y + y_shift,
            {disk_name .. string.rep(" ", space_len) .. str_disk_perc},
            {default_fg_color},
            font_name,
            font_size
        )
        draw_right_text(cr, 0, disk_y + y_shift + 40, {str_disk}, {default_fg_color}, font_name, font_size)
    else
        draw_center_text(
            cr,
            disk_x,
            disk_y + y_shift,
            disk_name .. string.rep(" ", space_len) .. str_disk_perc,
            default_fg_color,
            font_name,
            font_size
        )
        draw_center_text(cr, disk_x, disk_y + y_shift + 40, str_disk, default_fg_color, font_name, font_size)
    end
end

function conky_main()
    local function setup_rings(cr, pt)
        local str = string.format("${%s %s}", pt["name"], pt["arg"])
        local value = tonumber(conky_parse(str)) -- 执行一条指令
        draw_ring(cr, value, pt)
    end

    if conky_window == nil then
        return
    end

    local cs =
        cairo_xlib_surface_create(
        conky_window.display,
        conky_window.drawable,
        conky_window.visual,
        conky_window.width,
        conky_window.height
    )
    local cr = cairo_create(cs)
    cairo_surface_destroy(cs)

    local days_in_month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

    local t = os.date("*t", os.time())
    local year, month, currentday, wday = t.year, t.month, t.day, t.wday

    -- 判断闰年
    local LeapYear = function(year)
        return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
    end

    if LeapYear(year) then
        days_in_month[2] = 29
    end
    rings_table[6]["max"] = days_in_month[month] -- 设置当月的天数

    -- 读取温度文件夹名称 lua中执行sed匹配与在终端中略有区别
    local hwmon = io.popen("ls /sys/devices/platform/coretemp.0/hwmon/ | sed 's/.*hwmon(*)/\1/g'"):read("*all")
    rings_table[8]["arg"] = "coretemp.0/hwmon/" .. hwmon .. " temp 1"

    -- 读取网络接口名称
    local wired = io.popen("ip addr | grep -n ' en' | cut -d ' ' -f 2 | cut -d ':' -f 1"):read("*all")
    rings_table[29]["arg"] = wired
    rings_table[30]["arg"] = wired
    local wifi = io.popen("ip addr | grep -n ' wlp' | cut -d ' ' -f 2 | cut -d ':' -f 1"):read("*all")
    rings_table[31]["arg"] = wifi
    rings_table[32]["arg"] = wifi

    -- Conky已运行5秒 画出全部圆环
    if tonumber(conky_parse("${updates}")) > 5 then
        for i in pairs(rings_table) do
            setup_rings(cr, rings_table[i])
        end
    end

    local day_name = {"日", "月", "火", "水", "木", "金", "土"}
    local big_name = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "廿", "卅"}

    local function to_large(num) -- 数字转大写汉字
        if num <= 10 then
            return big_name[num]
        elseif num < 20 then
            return big_name[10] .. big_name[num - 10]
        elseif num == 20 then
            return big_name[11]
        elseif num < 30 then
            return big_name[11] .. big_name[num - 20]
        elseif num == 30 then
            return big_name[12]
        else
            return big_name[12] .. big_name[num - 30]
        end
    end

    local y_info_start = 120
    local y_info_inc = 60
    local y_info_inc2 = 40

    draw_right_text(
        cr,
        0,
        y_info_start,
        {to_large(month) .. "-" .. to_large(currentday) .. " " .. day_name[wday]},
        {default_fg_color},
        "OperatorFZLongZhao",
        default_font_size + 10
    )

    draw_right_text(
        cr,
        0,
        y_info_start + y_info_inc,
        {conky_parse("${nodename}")},
        {default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_right_text(
        cr,
        0,
        y_info_start + y_info_inc + y_info_inc2,
        {
            io.popen("cat /proc/version | cut -d ' ' -f 3 | cut -d '-' -f 1"):read("*all"):gsub("\n", ""),
            "-" .. io.popen("cat /proc/version | cut -d ' ' -f 3 | cut -d '-' -f 2-3"):read("*all"):gsub("\n", "")
        },
        {emph_color, default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    local update_time = conky_parse("${uptime}")
    draw_right_text(
        cr,
        0,
        y_info_start + y_info_inc + 2 * y_info_inc2,
        {update_time},
        {default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_right_text(
        cr,
        0,
        y_info_start + y_info_inc + 3 * y_info_inc2,
        {conky_parse("${execi 3600 checkupdates | wc -l}"), "+updates"},
        {emph_color, default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_right_text(
        cr,
        0,
        y_info_start + y_info_inc + 4 * y_info_inc2,
        {conky_parse("${processes}") .. " processes"},
        {default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_center_text(
        cr,
        clock_x,
        clock_y,
        conky_parse("${time %H:%M}"),
        default_fg_color,
        "OperatorFZLongZhao",
        default_font_size
    )

    draw_center_text(cr, cpu_x, cpu_y, "核", default_fg_color, "OperatorFZLongZhao", default_font_size + 10)

    cpu_perc = conky_parse("${cpu cpu0}") .. "%"
    cpu_temp = conky_parse("${platform coretemp.0/hwmon/" .. hwmon .. " temp 1}") .. "°C"

    draw_right_text(
        cr,
        0,
        cpu_y - 35,
        {cpu_perc, "+/+", cpu_temp},
        {emph_color, default_fg_color, emph_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_process_text(cr, cpu_x, cpu_y, 5, "cpu", "Operator Mono Light", default_font_size - 6) -- 画 cpu top5
    draw_process_text(cr, mem_x, mem_y, 5, "mem", "Operator Mono Light", default_font_size - 6) -- 画 ram top5

    draw_line(cr, cpu_x + 114, cpu_y, conky_window.width, cpu_y, 2, cpu_bg_alpha)
    draw_line(cr, mem_x + 114, mem_y, conky_window.width, mem_y, 2, cpu_bg_alpha)

    local mem_used = conky_parse("$mem"):gsub("iB", "")
    local mem_max = conky_parse("$memmax"):gsub("iB", "")
    local mem_perc = conky_parse("$memperc"):gsub("iB", "")
    local mem_usage = {mem_used, "+/+", mem_max, "+(", mem_perc, "%", ")"}
    draw_right_text(
        cr,
        0,
        mem_y - 35,
        mem_usage,
        {emph_color, default_fg_color, default_fg_color, default_fg_color, emph_color, emph_color, default_fg_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    draw_center_text(cr, mem_x, mem_y, "存", default_fg_color, "OperatorFZLongZhao", default_font_size + 10)

    draw_center_text(cr, disk_x, disk_y, "磁", default_fg_color, "OperatorFZLongZhao", default_font_size + 10)

    draw_disk_text(cr, 70, -60, "/home", "home", "Operator Mono Light", default_font_size - 6, "r")

    draw_disk_text(cr, -50, 100, "/efi", "efi", "Operator Mono Light", default_font_size - 6, "c")

    draw_disk_text(cr, -250, -60, "/", "root", "Operator Mono Light", default_font_size - 6, "l")

    draw_center_text(cr, net_x, net_y, "网", default_fg_color, "OperatorFZLongZhao", default_font_size + 10)

    local ethernet_addr = conky_parse("${addrs " .. wired .. "}")
    draw_left_text(cr, 0, net_y + 10, ethernet_addr, emph_color, "Operator Mono Light", default_font_size - 6)

    local wifi_addr = conky_parse("${addrs " .. wifi .. "}")
    draw_right_text(
        cr,
        net_x + ip_shift,
        net_y + 10,
        {wifi_addr},
        {emph_color},
        "Operator Mono Light",
        default_font_size - 6
    )

    local ethernet_up = conky_parse("${upspeed " .. wired .. "}/s (${totalup " .. wired .. "})"):gsub("iB", "")
    draw_left_text(cr, 0, net_y - 120, ethernet_up, default_fg_color, "Operator Mono Light", default_font_size - 6)

    local ethernet_down = conky_parse("${downspeed " .. wired .. "}/s (${totaldown " .. wired .. "})"):gsub("iB", "")
    draw_left_text(cr, 0, net_y + 140, ethernet_down, default_fg_color, "Operator Mono Light", default_font_size - 6)

    local wifi_up = conky_parse("${upspeed " .. wifi .. "}/s (${totalup " .. wifi .. "})"):gsub("iB", "")
    draw_right_text(cr, 330, net_y - 120, {wifi_up}, {default_fg_color}, "Operator Mono Light", default_font_size - 6)

    local wifi_down = conky_parse("${downspeed " .. wifi .. "}/s (${totaldown " .. wifi .. "})"):gsub("iB", "")
    draw_right_text(cr, 330, net_y + 140, {wifi_down}, {default_fg_color}, "Operator Mono Light", default_font_size - 6)

    cairo_destroy(cr)
end
